#!perl -w

opendir(local *D,".");
my @List =
  sort { 
    my ($an) = $a=~m/(\d+)/s;
    my ($bn) = $b=~m/(\d+)/s;
    if(!$an) { -1 }
    elsif(!$bn) { 1 }
    else { ($an+0) <=> ($bn+0) }
  } 
  grep { !/^\./ && /shtml/ } 
  readdir(D);

die "You must place this script to the main fucked SHTML directory!\n" if !@List;

open(local *F, ">all.html");

my $i=0;
foreach my $fn (@List) {
	local ($/,$\);
	open(local *FI, $fn) or next;
	my $st=<FI>;
	if($i!=0) {
		$st=~s{^.*?</title>}{}si;
	} else {
		$st=~s{(</title>\s*)}{
			$1<style>
			p { margin-top: 0 pt; text-indent: 30 pt; text-align: justify;}
			</style>
			</head>
			<font size=2>
			<body leftmargin=60>
		}sig;
	}
	$st=~s{<p><center><a href="(contents|glava).*}{}si;
	$st=~s{(</body>)?\s*</html>.*}{}si;
	$st=~s{glava_\d+\.shtml#}{#}sgi;
	$st=~s{<pre>(.*?)</pre>}{
		my $st=$1;
		$st=~s/(\r?)\n\r?\n/$1\n/sgi;
		"<pre>$st</pre>";
	}sgie;
	$st=~s{(<img\s+src\s*=\s*")\.\./\.\./pictures/it/osbd}{$1img}sgi;
	print F "<!-- $fn -->\n";
	print F $st;
} continue {
	$i++;
}

